/*
 * Licensed under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.submarine.exchange;

import java.util.HashMap;

import org.submarine.common.Constants;
import org.submarine.common.ServiceLoaderHelper;
import org.submarine.common.URL;
import org.submarine.config.ConfigUtil;
import org.submarine.registry.AbstractRegistry;
import org.submarine.registry.RegistryFactory;
import org.submarine.transport.Endpoint;
import org.submarine.util.NetUtil;

/**
 * @file org.submarine.exchange.Collector.java
 *
 * Collector
 * 
 * @author gaojian
 * @created 2013年8月25日 下午3:29:42
 * @version 0.0.1
 * @since 0.0.1
 * 
 */
public class Collector {
	
	private boolean running = true;
	
	private synchronized Endpoint getServer(URL url) {
		return ServiceLoaderHelper.ENDPOINT_FACTORY.iterator().next().getServer(url, ExchangeFactory.getReciver());
	}

	public void start() {
		RegistryFactory registryFactory = ServiceLoaderHelper.REGISTRY_FACTORY.iterator().next();
		AbstractRegistry registry = registryFactory.createRegistry(URL.localURL());
		URL localURL = new URL("userName", "password", NetUtil.getHost(), 
				Integer.parseInt(ConfigUtil.getProperty(Constants.APPLICATION_PORT_KEY)), null, 
				Constants.COLLECTOR_APPLICATION_SIDE, new HashMap<String, String>());
		registry.register(localURL);
		getServer(URL.localURL()).start();
//		Runtime.getRuntime().addShutdownHook(new Thread() {
//			public void run() {
//				System.out.println("ShutdownHook");//FIXME(to delete)
//				try {
//					Collector.this.stop();
//					running = false;
//				} catch (Throwable t) {
//					t.printStackTrace();
//				}
//			}
//		});
	}

//	public Response invokeSync(Object data) throws InterruptedException {}
	
	/**
	 * @return the running
	 */
	public boolean isRunning() {
		return running;
	}

}
